package codeTop;

import java.util.HashMap;
import java.util.Map;

public class q3 {
    public int lengthOfLongestSubstring(String s) {
        int n = s.length();
        int idx = 0;
        int length = 0;
        int maxLength = 0;
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            if (!map.containsKey(s.charAt(i))||map.get(s.charAt(i))<idx) {
                map.put(s.charAt(i), i);
                length++;
                maxLength = Math.max(length, maxLength);
            }else {
                idx = map.get(s.charAt(i))+1;
                map.put(s.charAt(i), i);
                length = i-idx+1;
            }
        }
        return maxLength;
    }

    public static void main(String[] args) {
        q3 q3 = new q3();
        System.out.println(q3.lengthOfLongestSubstring("tmmzuxt"));
    }
}
